home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / newmat08.zip / SORT.CPP < prev    next >
C/C++ Source or Header  |  1995-01-11  |  1KB  |  54 lines

  1. //$$ sort.cpp                            Sorting
  2.  
  3. // Copyright (C) 1991,2,3,4: R B Davies
  4.  
  5. #define WANT_MATH
  6.  
  7. #include "include.h"
  8.  
  9. #include "newmatap.h"
  10.  
  11.  
  12. /******************************** Shell sort ********************************/
  13.  
  14. void SortAscending(GeneralMatrix& GM)
  15. {
  16.    // from numerical recipies in C - Shell sort
  17.    Tracer et("Sort-ascending");
  18.  
  19.    const double aln2i = 1.442695022; const double tiny = 1.0e-5;
  20.    Real* gm = GM.Store(); int n = GM.Storage(); int m = n;
  21.    int lognb2 = (int)(aln2i * log((double)n) + tiny);
  22.    while (lognb2--)
  23.    {
  24.       m >>= 1;
  25.       for (int j = m; j<n; j++)
  26.       {
  27.          Real* gmj = gm+j; int i = j-m; Real* gmi = gmj-m; Real t = *gmj;
  28.          while (i>=0 && *gmi>t)  { *gmj = *gmi; gmj = gmi; gmi -= m; i -= m; }
  29.          *gmj = t;
  30.       }
  31.    }
  32. }
  33.  
  34. void SortDescending(GeneralMatrix& GM)
  35. {
  36.    // from numerical recipies in C - Shell sort
  37.    Tracer et("Sort-descending");
  38.  
  39.    const double aln2i = 1.442695022; const double tiny = 1.0e-5;
  40.    Real* gm = GM.Store(); int n = GM.Storage(); int m = n;
  41.    int lognb2 = (int)(aln2i * log((double)n) + tiny);
  42.    while (lognb2--)
  43.    {
  44.       m >>= 1;
  45.       for (int j = m; j<n; j++)
  46.       {
  47.          Real* gmj = gm+j; int i = j-m; Real* gmi = gmj-m; Real t = *gmj;
  48.          while (i>=0 && *gmi<t)  { *gmj = *gmi; gmj = gmi; gmi -= m; i -= m; }
  49.          *gmj = t;
  50.       }
  51.    }
  52. }
  53.  
  54.